using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class Praise_messageDAL
    {
        public static int Save(Praise_messageInfo info)
        {
            string sql = string.Format("insert into [praise_message]([ArticleID],[UserID],[MessageID],[IsPraise]) values(@ArticleID,@UserID,@MessageID,@IsPraise);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@MessageID", info.MessageID),
                new SqlParameter("@IsPraise", info.IsPraise)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(Praise_messageInfo info)
        {
            string sql = string.Format("update [praise_message] set [ArticleID]=@ArticleID,[MessageID]=@MessageID,[IsPraise]=@IsPraise where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@MessageID", info.MessageID),
                new SqlParameter("@IsPraise", info.IsPraise),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static Praise_messageInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[praise_message]");
            return SimpleData(dr);
        }
        public static Praise_messageInfo FindOneByUserIDAndArticleID(int userid, int articleid,int messageid)
        {
            return AllData(CommonDAL.GetDataRow(string.Format("select * from praise_message where UserID={0} and ArticleID={1}and messageid={2} ", userid, articleid, messageid)));
        }
        public static List<Praise_messageInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[praise_message]");
            return ListSimpleData(dt);
        }
        public static List<Praise_messageInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [praise_message].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [praise_message].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter.ToString(), "[praise_message]", out count);
            return ListSimpleData(dt);
        }
        public static List<Praise_messageInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [praise_message]"));
            return ListSimpleData(dt);
        }
        private static Praise_messageInfo SimpleData(DataRow dr)
        {
            Praise_messageInfo info = new Praise_messageInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ArticleID = DataConvert.ToInt(dr["ArticleID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.MessageID = DataConvert.ToInt(dr["MessageID"]);
                info.IsPraise = DataConvert.ToBool(dr["IsPraise"]);
            }
            return info;
        }
        private static ExtraPraise_messageInfo AllData(DataRow dr)
        {
            ExtraPraise_messageInfo info = new ExtraPraise_messageInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ArticleID = DataConvert.ToInt(dr["ArticleID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.MessageID = DataConvert.ToInt(dr["MessageID"]);
                info.IsPraise = DataConvert.ToBool(dr["IsPraise"]);
            }
            return info;
        }
        private static List<Praise_messageInfo> ListSimpleData(DataTable dt)
        {
            List<Praise_messageInfo> list = new List<Praise_messageInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ExtraPraise_messageInfo> ListAllData(DataTable dt)
        {
            List<ExtraPraise_messageInfo> list = new List<ExtraPraise_messageInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

